package com.saret.crawler.parser;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.saret.crawler.type.GWord;
import com.saret.crawler.type.GWordList;
import com.saret.utils.FileLocator;
import com.saret.utils.UtfFileHandle;

import java.io.File;
import java.util.List;

/**
 * @author biniam.gebremichael
 * @since 11/23/11
 */
public class GeezParser extends TxtParser {

    public static final int GEEZ_START = 4608;
    public static final int GEEZ_END = 4954;
    public static final int GEEZ_APOSTROPH = 8217;
    List<String> badSeed;

    @Inject
    public GeezParser(
            @Named("saret.crawler.current.wordlList") String wordListFile,
            @Named("saret.crawler.seed.bad") String badSeedFile) {
        super(wordListFile);
        badSeed = UtfFileHandle.readFileToListOfLines(FileLocator.getConfigFile(badSeedFile.split("/")));
    }

    @Override
    protected GWordList wordListVisitor(GWordList word, File file) {
        for (GWord gWord : word.getgWordMap()) {
            for (String s : badSeed) {
                if (gWord.getWord().equals(s)) {
                    System.out.println(file.getName() + " contains bad word like " + s);
                    return new GWordList();
                }
            }
        }
        System.out.println("YES" + word.getgWordMap().size() + " " + file);

        return word;
    }


    @Override
    protected String wordVisitor(String wd) {

        String word = trimLatin(wd);

        if (isBrokenPdf(word)) {
            return null;
        } else {
            return removeMixedWord(removeNonUnicode(word));
        }
    }

    public static String removeMixedWord(String word) {

        if (word == null) return null;

        for (int unicodeVal : word.toCharArray()) {

            if (unicodeVal <= GEEZ_START || unicodeVal >= GEEZ_END) {
                if (unicodeVal != GEEZ_APOSTROPH) {
                    return null;
                }
            }
        }


        return word;
    }

    public static String trimLatin(String word) {
        if (word == null) return null;
        char[] wordArray = word.toCharArray();
        for (int unicodeVal : wordArray) {
            if (unicodeVal >= GEEZ_START && unicodeVal <= GEEZ_END) {
                return word;
            }
        }
        return null;
    }

    public static String removeNonUnicode(String word) {

        if (word == null) return null;

        int start = -1;
        int end = 0;
        char[] wordArray = word.toCharArray();
        for (int i = 0; i < wordArray.length; i++) {
            int unicodeVal = wordArray[i];
            if (unicodeVal >= GEEZ_START && unicodeVal <= GEEZ_END) {
                if (start < 0) {
                    start = i;
                }
                end = i;
            }
        }

        if (start < 0 || end - start < 1) {
            return null;
        } else {
            return word.substring(start, end + 1);
        }
    }

    public static boolean isBrokenPdf(String word) {

        if (word == null) return false;

        for (char ch : word.toCharArray()) {
            if (ch == 'E' || ch == 'A' || ch == 'U') {
                return true;
            }
        }
        return false;

    }

}
